<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/9
 * Time: 15:06
 */

namespace app\admin\model;


use app\admin\helper\LangHelper;
use app\admin\helper\SearchHelper;
use app\admin\popo\LayData;
use think\Config;
use think\Request;
use think\Session;

class ResourceButtonM extends BaseM
{
    protected $table = 'tb_resource_button';
    protected $module = 'BUTTON资源';

    /*
     * 授权
     * */
    public function auth(){
        $role_id=input("post.role_id/s");
        $auths=input("post.auths/a");
        $data=[];
        foreach($auths as $value){
            $controller = explode('-',$value)[0];
            $action = explode('-',$value)[1];
            $data[]=array(
                "menu"=>$controller,
                "button"=>$action,
                "role_id"=>$role_id,
                "updated_at"=>time(),
            );
        }
        //事务开始
        $this->startTrans();
        $count=$this->where("role_id='$role_id'")->count();
        //先删除role_id相关的所有resource
        if($this->where("role_id='$role_id'")->delete() == $count){
            if($this->insertAll($data)){
                $this->commit();
                $last_sql = Config::get('app_debug')?$this->getLastSql():'';
                $last_sql_1 = $this->getLastSql();
                //当前登录用户role_id等于修改则加载button-resource
                if (Session::get('role_id') == $role_id ) {
                    $buttonResource = $this->listAll($role_id);
                    Session::set("button-resource",$buttonResource);
                }
                $log = [
                    'op_uid' => Session::get('username'),
                    'op_module' => $this->module,
                    'op_type' => 'auth',
                    'op_origin' => $this->table,
                    'op_data' => json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
                    'op_ip' => getIP(),
                    'op_sql' => $last_sql_1,
                ];
                LogsM::add($log);
                pr_log($log,'operate');
                return op_success($last_sql);
            }
        }
        $this->rollback();
        return op_fail($this->getLastSql());
    }

    /**
     * 删除
     * @param $role_id
     * @return int
     */
    public static function del($role_id){
        $mod = new self();
        $role_id = "'".implode("','",$role_id)."'";
        return $mod->where("role_id in ($role_id) ")->delete();
    }

    /*
     * 列表
     * */
    public function listAll($role_id){
        $res = [];
        $resoure = $this->where('role_id',$role_id)->select();
        foreach ($resoure as $v){
            $res[] = $v['menu'].'-'.$v['button'];
        }
        if (Request::instance()->isGet()) {
            $log = [
                'op_uid' => Session::get('username'),
                'op_module' => $this->module,
                'op_type' => 'search',
                'op_origin' => $this->table,
                'op_data' => 'find one',
                'op_ip' => getIP(),
                'op_sql' => $this->getLastSql(),
            ];
            LogsM::add($log);
            pr_log($log,'operate');
        }
        return $res;
    }

}